<?php
if (!defined('__COLA_DIR__')) exit;
/**
 * 注册组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Users_Register extends Widget_Abstract_Users
{
    /**
     * 初始化函数
     *
     * @access public
     * @return void
     */
    public function action()
    {
        /** 如果已经登录 */
        if ($this->user->hasLogin() || !$this->options->allowRegister) {
			
            /** 设置提示信息 */
            $this->result->set(-1, _t('已经登录'), array('location'=>$this->options->ucenterUrl));
        }
		
        $this->security->protect();

		$form = $this->form();

		/** 验证表单 */
        if ($message = $form->validate()) {
            $this->result->set(key($message), current($message));
        }

        $user = $this->request->from(array_keys($form->getInputs()));
        $user['nickName'] = empty($user['nickName']) ? $user['name'] : $user['nickName'];
        $user['created'] = $this->options->time;		
		$user['group'] = 'member';	

		/** 插件接口 */
		//HOOK $user | PLUG_Register($user, $this);

        $insertId = $this->insert($user);
        $this->select()
		   ->where('uid = ?', $insertId)
           ->limit(1)
		   ->fetchRow(array($this, 'push'));

		/** 插件接口 */   
		//HOOK PLUG_Register_Success($this);

        $this->user->login($this->request->name, $this->request->password);
		
        $this->result->set(0, _t('用户 <strong>%s</strong> 已经成功注册, 密码为 <strong>%s</strong>', $this->nickName, $this->request->password), array('location'=>$this->options->ucenterUrl));
    }	

    /**
     * 生成表单
     *
     * @access public
     * @return Form
     */
    public function form()
    {
        /** 构建表格 */
        $form = new Form($this->security->actionUrl('register', false), Form::POST_METHOD);

        /** 用户name */
        $name = new Form_Text('name', NULL, NULL, _t('用户名'));
        $form->addInput($name);

        /** mail */
        $mail = new Form_Text('mail', NULL, NULL, _t('邮箱'));
        $form->addInput($mail);
		
        /** password */
        $password = new Form_Text('password', NULL, NULL, _t('密码'));
        $form->addInput($password);

        /** 用户动作 */
        $do = new Form_Hidden('do', NULL, 'login');
        $form->set($do);

        /** 提交按钮 */
        $submit = new Form_Submit('submit', NULL, _t('注册'));
        $form->set($submit);

        /** 给表单增加规则 */
        $name->addRule('required', _t('必须填写用户名称'));
        $name->addRule('minLength', _t('用户名至少包含2个字符'), 2);
        $name->addRule('maxLength', _t('用户名最多包含32个字符'), 32);
        $name->addRule('xssCheck', _t('请不要在用户名中使用特殊字符'));
        $name->addRule(array($this, 'nameExists'), _t('用户名已经存在'));
		
        $mail->addRule('required', _t('必须填写电子邮箱'));
        $mail->addRule(array($this, 'mailExists'), _t('电子邮箱地址已经存在'));
        $mail->addRule('email', _t('电子邮箱格式错误'));
        $mail->addRule('maxLength', _t('电子邮箱最多包含64个字符'), 64);
		
        $password->addRule('required', _t('必须填写密码'));
        $password->addRule('minLength', _t('为了保证账户安全, 请输入至少六位的密码'), 6);
        $password->addRule('maxLength', _t('为了便于记忆, 密码长度请不要超过十八位'), 18);
        //$password->addRule('confirm', 'confirm', _t('两次输入的密码不一致'), 'password');

		/** 插件接口 */
		//HOOK PLUG_Register_Form($form, $this);
		
        return $form;
    }

}
